<template>
  <div class="search-panel">
    <el-row class="m-header-searchbar">
      <el-col :span="3" class="left">
        <img src="../../../assets/img/logo.png" @click="goToIndex" alt="大风车" title="大风车app" />
        <span class="left-title">大风车</span>
      </el-col>
      <el-col :span="15" class="center">
        <div class="wrapper">
          <el-input
            v-model="search"
            placeholder="搜索商家或地点"
            @focus="focus"
            @blur="blur"
            @input="input"
          ></el-input>
          <button class="el-button el-button--primary">
            <i class="el-icon-search"></i>
          </button>
          <dl class="hotPlace" v-if="isHotPlace">
            <dt>热门搜索</dt>
            <dd v-for="(item,idx) in hotPlace" :key="idx">
              <a :href="'/products?keyword='+encodeURIComponent(item.name)">{{item.name}}</a>
            </dd>
          </dl>
          <dl class="searchList" v-if="isSerachList">
            <dd v-for="(item,idx) in searchList" :key="idx">
              <a :href="'/products?keyword='+encodeURIComponent(item.name)">{{item.name}}</a>
            </dd>
          </dl>
        </div>
        <p class="suggest">
          <a
            :href="'/products?keyword='+encodeURIComponent(item.name)"
            v-for="(item,idx) in hotPlace"
            :key="idx"
          >{{item.name}}</a>
        </p>
        <ul class="nav">
          <li>
            <nuxt-link to="/" class="takeout">大风车外卖</nuxt-link>
          </li>
          <li>
            <nuxt-link to="/" class="movie">猫眼电影</nuxt-link>
          </li>
          <li>
            <nuxt-link to="/" class="hotel">大风车酒店</nuxt-link>
          </li>
          <li>
            <nuxt-link to="/" class="apartment">民宿 / 公寓</nuxt-link>
          </li>
          <li>
            <nuxt-link to="/" class="business">商家入驻</nuxt-link>
          </li>
        </ul>
      </el-col>
      <el-col :span="6" class="right">
        <ul class="security">
          <li>
            <i class="refund" />
            <p class="txt">随时退</p>
          </li>
          <li>
            <i class="single" />
            <p class="txt">不满意免单</p>
          </li>
          <li>
            <i class="overdue" />
            <p class="txt">过期退</p>
          </li>
        </ul>
      </el-col>
    </el-row>
  </div>
</template>
<script>
import Lodash from "lodash";
import { async } from "q";
export default {
  name: "searchbar",
  data() {
    return {
      search: "",
      isFoucs: false,
      hotPlace: [],
      searchList: []
    };
  },
  computed: {
    isHotPlace() {
      return this.isFoucs && !this.search;
    },
    isSerachList() {
      return this.isFoucs && this.search;
    }
  },
  created() {
    let city = this.$store.state.geo.position.city.replace("市", "");
    this.$axios
      .get("/search/hotPlace", {
        params: {
          city
        }
      })
      .then(res => {
        this.hotPlace = res.data.result.slice(0, 5);
      });
  },
  methods: {
    focus() {
      this.isFoucs = true;
    },
    blur() {
      setTimeout(() => {
        this.isFoucs = false;
      }, 200);
    },
    input: Lodash.debounce(async function() {
      let self = this;
      let city = self.$store.state.geo.position.city.replace("市", "");
      self.searchList = [];
      let {
        status,
        data: { top }
      } = await self.$axios.get("/search/top", {
        params: {
          input: self.search,
          city
        }
      });
      self.searchList = top.slice(0, 10);
    }, 300),
    goToIndex() {
      this.$router.push("/");
    }
  }
};
</script>
<style lang="scss">
</style>
